package com.klikmee.sms.core.base;

import java.text.MessageFormat;
import java.util.List;

import org.apache.log4j.Logger;

import com.klikmee.sms.core.model.ServiceChargingCategory;

/**
 * Service communicator that sends only single bulk messages (ignores other
 * recipients if any)
 * 
 * @author makcro
 * 
 */
public class SingleBulkServiceCommunicatorImpl extends SimpleServiceCommunicatorImpl {

	// logger instance
	private Logger	logger	= Logger.getLogger( SingleBulkServiceCommunicatorImpl.class );

	/**
	 * Override of {@link #sendBulkMessage(String, List, String, Long, Long)}
	 * that sends bulk to only one recipient (the first) and as a result returns
	 * core id of created bulk message
	 */
	@Override
	public int sendBulkMessage(String bulkMessageText, List<String> phoneNumbers, String signatureText, Long serviceId, Long chargingId) {
		// verify charging info
		int verificationResult = verifyChargingInformation( serviceId, chargingId );

		if (verificationResult != 0) {
			return verificationResult;
		}
		logger.debug( "Bulk request verification succeeded, instructing bulk creator to produce bulk message" );

		ServiceChargingCategory chargingCategory = chargingDAO.find( chargingId );

		// use only first recipient
		List<String> singleRecipient = phoneNumbers.subList( 0, 1 );

		int coreId = bulkMessageCreator.createBulkMessage( bulkMessageText, singleRecipient, signatureText, chargingCategory );
		logger.debug( MessageFormat.format( "Creator produced bulk message with coreId [{0}]", coreId ) );

		return coreId;

	}
}
